<?php
/*
	收入明细
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_SESSION['role_id']>6 && $_SESSION['role_id']<15){
	exi('您无权访问！','error');
}
if($_RQ['op']=='list' || $_RQ['op']=='list2'){
	$params=array('type'=>array(1,4),'status'=>array(1,3,4,5),'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['op']=='list2'){
		$params['type']=array(2,5);
	}
	if($_SESSION['role_id']==5 || $_SESSION['role_id']==6){
		$params['worker'.$_SESSION['role_id'].'id']=$_SESSION['workerid'];
	}
	$return['params']['type']=intval($_RQ['type']);
	$return['params']['status']=intval($_RQ['status']);
	$params['uniacid']=$_SESSION['uniacid'];
	if($_RQ['storeid']){
		$params['storeid']=$_RQ['storeid'];
		$params2['storeid']=$_RQ['storeid'];
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if($_RQ['worker5id']){
		$params['worker5id']=intval($_RQ['worker5id']);
		$return['params']['worker5id']=$_RQ['worker5id'];
	}
	if($_RQ['worker6id']){
		$params['worker6id']=intval($_RQ['worker6id']);
		$return['params']['worker6id']=$_RQ['worker6id'];
	}
	if($_RQ['title']){
		$params2['title like']='%'.trim($_RQ['title']).'%';
		$memberids=pdo_getall('jy_yxw_member',$params2,'id','id');
		if($memberids){
			$memberids=array_keys($memberids);
		}else{
			$memberids=-1;
		}
		$params['memberid']=$memberids;
		$return['params']['title']=$_RQ['title'];
	}
	if($_RQ['cardno']){
		$params2['cardno like']='%'.trim($_RQ['cardno']).'%';
		$memberids=pdo_getall('jy_yxw_member',$params2,'id','id');
		if($memberids){
			$memberids=array_keys($memberids);
		}else{
			$memberids=-1;
		}
		$params['memberid']=$memberids;
		$return['params']['cardno']=$_RQ['cardno'];
	}
	if($_RQ['mstatus']){
		$params2['status']=$_RQ['mstatus'];
		$memberids=pdo_getall('jy_yxw_member',$params2,'id','id');
		if($memberids){
			$memberids=array_keys($memberids);
		}else{
			$memberids=-1;
		}
		$params['memberid']=$memberids;
		$return['params']['cardno']=$_RQ['cardno'];
	}
	if($_RQ['jiao_day']){
		$params['jiao_day']=$_RQ['jiao_day'];
		$return['params']['jiao_day']=$_RQ['jiao_day'];
	}
	if($_RQ['zhanghu']){
		$params['zhanghu']=$_RQ['zhanghu'];
		$return['params']['zhanghu']=$_RQ['zhanghu'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['jiao_time between']=array($time_range['tstart'],$time_range['tend']);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:50;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$total='';
	$list=pdo_getslice('jy_yxw_infee_log',$params,array($pindex,$psize),$total,$fields,'','jiao_time desc,id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=$this->model('member')->member_show($item['memberid']);
			$member=pdo_get('jy_yxw_member',array('id'=>$item['memberid'],'uniacid'=>$_SESSION['uniacid']),array('title','cardno','worker5id','worker6id'));
			$item['worker6title']=pdo_getcolumn('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['worker6id']?:$member['worker6id']),'title');
			$item['worker5title']=pdo_getcolumn('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['worker5id']?:$member['worker5id']),'title');
			$item['createtime']=date('Y-m-d',$item['createtime']);
			if(!$item['status'] && $item['tofee']>0){
				$item['fee']=$item['tofee'];
			}
			if((!$item['worker6id'] || $member['worker6id']!=$item['worker6id']) && $member['worker6id']){
				$this->update('infee_log',array('worker6id'=>$member['worker6id'],'up_time'=>time()),array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['id']));
			}
			if(!$item['worker5id'] && $member['worker5id']){
				$this->update('infee_log',array('worker5id'=>$member['worker5id'],'up_time'=>time()),array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['id']));
			}
			$item['worker6title']=$item['worker6title']?:'无';
			if($item['workerheid']){
				$workerhe=pdo_get('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['workerheid']),array('role','title'));
				$item['workerhetitle']=$this->user_role[$workerhe['role']].':'.$workerhe['title'];
			}
			if($item['status']==1){
				$isfeelog=pdo_get('jy_yxw_feelog',array('infeeid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				if(!$isfeelog['id']){
					$this->model('feelog')->upfeelog($item['id'],'',$item['type']==2?1:0);
				}
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[<red>温馨提示</red>]：这里只显示成交记录（不显示退款的，定金等）；','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'用户信息','type'=>'member'),
			array('name'=>'storeid','title'=>'所属门店','type'=>'select2','options'=>'stores'),
			array('name'=>'fee','title'=>'金额','type'=>'string'),
			array('name'=>'createtime','title'=>'申请日期','type'=>'string'),
			array('type'=>'radio2','title'=>'审批状态','name'=>'status','options'=>'status'),
			array('name'=>'pay_type','title'=>'交费方式','type'=>'radio2','options'=>'pay_types'),
			array('name'=>'daipay_type','title'=>'交费渠道','type'=>'radio2','options'=>'daipay_types','show'=>array('pay_type'=>array(2,3))),
			array('name'=>'jiao_day','title'=>'交费日期','type'=>'string'),
			array('name'=>'zhanghu','title'=>'到账账户','type'=>'string'),
			array('type'=>'img_string','title'=>'合作销售',
            'data'=>array(
                array('title'=>'销售员:','name'=>'worker6title'),
                array('title'=>'','name'=>'workerhetitle'),
                )
            ),
			array('name'=>'worker5title','title'=>'电客','type'=>'string'),
			array('name'=>'jiaobenfee','title'=>'售后成本','type'=>'string'),
			array('name'=>'zufee','title'=>'著作权','type'=>'string'),
			array('name'=>'peifee','title'=>'培训费','type'=>'string'),
			array('name'=>'zhaofee','title'=>'招生费','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>10,'title'=>'姓名','type'=>'string'),
			array('name'=>'cardno','width'=>10,'title'=>'身份证号','type'=>'string'),
			array('name'=>'mstatus','width'=>10,'title'=>'状态','type'=>'radio','options'=>'mstatus'),
			//array('name'=>'jiao_day','width'=>10,'title'=>'交费日期','type'=>'time','time_type'=>'day'),
			array('name'=>'zhanghu','width'=>10,'title'=>'到账账户','type'=>'string'),
			array('name'=>'createtime','width'=>15,'title'=>'申请日期','type'=>'time_range'),
		)
	);
	$return['status']=array('待审批','审批通过','审批不通过');
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_SESSION['storeids']){
		$params['id']=$_SESSION['storeids'];
	}
	$return['stores']=$this->getall('store',$params,array('id','title'),'id');
	$return['workers6']=$this->getall('worker',array('role'=>6,'uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','title'),'id');
	$return['workers5']=$this->getall('worker',array('role'=>5,'uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','title'),'id');
	$return['htmls']=$htmls;
	$return['mstatus']=$this->member_status;
	$return['pay_types']=$this->pay_types;
	$return['daipay_types']=$this->daipay_types;
	exi($return);
}
if($_RQ['op']=='list4'){
	$return['params']=array();
	$params=$params2=array('uniacid'=>$_SESSION['uniacid']);
	if($_SESSION['role_id']==5 || $_SESSION['role_id']==6){
		$params['worker'.$_SESSION['role_id'].'id']=$_SESSION['workerid'];
	}
	if($_SESSION['storeids']){
		$params['storeid']=$_SESSION['storeids'];
	}
	if($_RQ['storeid']){
		$params['storeid']=$_RQ['storeid'];
		$params2['storeid']=$_RQ['storeid'];
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if($_RQ['worker5id']){
		$params['worker5id']=intval($_RQ['worker5id']);
		$return['params']['worker5id']=$_RQ['worker5id'];
	}
	if($_RQ['worker6id']){
		$params['worker6id']=intval($_RQ['worker6id']);
		$return['params']['worker6id']=$_RQ['worker6id'];
	}
	$params['type']=array(0,1);
	$params['status']=4;
	$return['params']['status']=intval($_RQ['status']);
	if($_RQ['title']){
		$params2['title like']='%'.trim($_RQ['title']).'%';
		$memberids=pdo_getall('jy_yxw_member',$params2,'id','id');
		if($memberids){
			$memberids=array_keys($memberids);
		}else{
			$memberids=-1;
		}
		$params['memberid']=$memberids;
		$return['params']['title']=$_RQ['title'];
	}
	if($_RQ['mobile']){
		$params2['mobile like']='%'.trim($_RQ['mobile']).'%';
		$memberids=pdo_getall('jy_yxw_member',$params2,'id','id');
		if($memberids){
			$memberids=array_keys($memberids);
		}else{
			$memberids=-1;
		}
		$params['memberid']=$memberids;
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['tuidaytime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:50;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$list=pdo_getslice('jy_yxw_tuifee_log',$params,array($pindex,$psize),$total,$fields,'','tuidaytime desc,id desc');
	if($list){
		foreach($list as $k=>&$item){
			$item['member']=$this->model('member')->member_show($item['memberid']);
			$item['type']=$item['type']?:1;
			if($item['status']==4){
				if($item['type']==2){
					//pdo_update('jy_yxw_infee_log',array('type'=>5,'status'=>4),array('status'=>array(1,4),'type'=>2,'uniacid'=>$_SESSION['uniacid'],'memberid'=>$item['memberid']));
				}elseif($item['type']==1 || !$item['type']){
					//pdo_update('jy_yxw_infee_log',array('type'=>4,'status'=>4),array('status'=>array(1,4),'type'=>1,'uniacid'=>$_SESSION['uniacid'],'memberid'=>$item['memberid']));
				}
			}
			if(!$item['tuidaytime'] && $item['tuiday']){
				pdo_update('jy_yxw_tuifee_log',array('tuidaytime'=>strtotime($item['tuiday'])),array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['id']));
			}
			if(!$item['worker5id'] && !$item['worker6id'] && !$item['workerheid']){
				$member=pdo_get('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['memberid']),array('worker5id','worker6id','workerheid','from1_type','from2_type'));
				$update=array(
					'worker5id'=>$member['worker5id'],
					'worker6id'=>$member['worker6id'],
					'workerheid'=>$member['workerheid'],
					'from1_type'=>$member['from1_type'],
					'from2_type'=>$member['from2_type'],
				);
				pdo_update('jy_yxw_tuifee_log',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['id']));
			}
			$member=pdo_get('jy_yxw_member',array('id'=>$item['memberid'],'uniacid'=>$_SESSION['uniacid']),array('title','cardno','worker5id','worker6id'));
			$item['worker6title']=pdo_getcolumn('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$member['worker6id']),'title');
			$item['worker5title']=pdo_getcolumn('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$member['worker5id']),'title');
			if($item['status']==4){
				$isfeelog=pdo_get('jy_yxw_feelog',array('tuifeeid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				if(!$isfeelog['id']){
					$this->model('feelog')->upfeelog($item['id'],'',$item['type']==2?4:3);
				}
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'已收取金额-退费金额=门店收益','total'=>$total,'psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'storeid','title'=>'所属门店','type'=>'select2','options'=>'stores'),
			array('name'=>'member','title'=>'客户信息','type'=>'member'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'type'),
			array('type'=>'unitstring','unit'=>'元','title'=>'已交金额','name'=>'fee'),
			array('type'=>'unitstring','unit'=>'元','title'=>'需收取金额','name'=>'isfee'),
			array('name'=>'remark','title'=>'退费原因','type'=>'string'),
			array('type'=>'string','title'=>'实际退费金额','name'=>'tuifee'),
			array('type'=>'string','title'=>'退费账户','name'=>'zhanghu'),
			array('type'=>'string','title'=>'退费日期','name'=>'tuiday'),
			array('name'=>'fee1','title'=>'总部退费分摊','type'=>'unitstring','unit'=>'元'),
			array('name'=>'fee2','title'=>'售后基地退费分摊','type'=>'unitstring','unit'=>'元'),
			array('name'=>'fee3','title'=>'招生门店退费分摊','type'=>'unitstring','unit'=>'元'),
			array('name'=>'worker6title','title'=>'销售员','type'=>'string'),
			array('name'=>'worker5title','title'=>'电客','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>10,'title'=>'姓名','type'=>'string'),
			array('name'=>'mobile','width'=>10,'title'=>'电话','type'=>'string'),
			array('name'=>'createtime','width'=>15,'title'=>'申请时间','type'=>'time_range'),
		),
	);
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_SESSION['storeids']){
		$params['id']=$_SESSION['storeids'];
	}
	$return['workers6']=$this->getall('worker',array('role'=>6,'uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','title'),'id');
	$return['stores']=$this->getall('store',$params,array('id','title'),'id');
	$return['type']=array(1=>'退成交',2=>'退定金');
	$return['status']=$this->tuibao_status;
	$return['htmls']=$htmls;	
	exi($return);
}